import {message, Modal, notification} from "antd";
import {useEffect} from "react";
import {useDispatch} from "react-redux";

let globalMessageApi: any;
let globalNotificationApi: any;
let globalModalApi: any;

let globalDispatch: any;


export function getModalSystemConfig(context: string){
    return {
        title: '系统提示!',
        content: (
            <span>{context}</span>
        ),
    };
}



/**
 * 用于弹出消息提示，系统级别的
 */
function AntdMessage() {
    const [messageApi, messageContextHolder] = message.useMessage();
    const [notificationApi, notificationContextHolder] = notification.useNotification();
    const [modal, contextHolder] = Modal.useModal();
    globalDispatch = useDispatch()
    useEffect(() => {
        globalMessageApi = messageApi
        globalNotificationApi = notificationApi
        globalModalApi = modal
    }, [messageApi, notificationApi])
    return (
        <>
            {messageContextHolder}
            {notificationContextHolder}
            {contextHolder}
        </>
    )
}

type Notification = {
    title?: string,
    content?: string,
    direction?: 'topRight' | 'bottomRight' | 'topLeft' | 'bottomLeft',
    type?: null | 'success' | 'warning' | 'error' | 'info' | undefined,
}

function showNotification(context: Notification): void {
    const messageObject = {
        message: context.title || '系统提示',
        description: context.content || '',
        placement: context.direction || 'topRight',
    };
    const type = context.type || 'success';
    switch (type) {
        case 'error':
            globalNotificationApi.error(messageObject);
            break;
        case 'warning':
            globalNotificationApi.warning(messageObject);
            break;
        case 'info':
            globalNotificationApi.info(messageObject);
            break;
        case 'success':
            globalNotificationApi.success(messageObject);
            break;
        default:
            console.error('不支持的消息类型');
            break;
    }
}


export {
    AntdMessage,
    showNotification,
    globalMessageApi,
    globalModalApi,
    globalNotificationApi,
    globalDispatch
}
